package com.itheima.bos.web.action;

import com.itheima.bos.domain.Region;
import com.itheima.bos.service.IRegionService;
import com.itheima.bos.utils.PinYin4jUtils;
import com.itheima.bos.web.action.base.BaseAction;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Mr.King on 2017/3/11.
 */
@Component
@Scope(value = "prototype")
public class RegionAction extends BaseAction<Region> {

    @Autowired
    private IRegionService regionService;

    //接收文件上传对象
    private File myfile;
    public void setMyfile(File myfile) {
        this.myfile = myfile;
    }

    //上传xls文件解析包村到数据库
    public String importXls() throws IOException {
        String flag = "1";// 导入是否成功的标识
        // 创建一个List对象，包装区域对象
        List<Region> list = new ArrayList<Region>();
        try {
            // 1、加载Excel文件
            HSSFWorkbook workbook = new HSSFWorkbook(
                    new FileInputStream(myfile));
            // 2、加载第一个sheet页
            HSSFSheet sheet = workbook.getSheetAt(0);
            // 3、循环sheet页，获得每一行数据
            for (Row row : sheet) {
                int rowNum = row.getRowNum();
                if (rowNum == 0) {
                    // 第一行为文件的标题行，忽略
                    continue;
                }
                // 4、获得当前行的某一列
                String id = row.getCell(0).getStringCellValue();
                String province = row.getCell(1).getStringCellValue();
                String city = row.getCell(2).getStringCellValue();
                String district = row.getCell(3).getStringCellValue();
                String postcode = row.getCell(4).getStringCellValue();
                // 将解析出的Excel文件中的数据包装成一个Region对象
                Region region = new Region(id, province, city, district,
                        postcode);
                System.err.println(id+" "+province+" "+city+" "+district+" "+postcode);

                // 使用Pinyin4J工具生成简码和城市编码

                // 简码----北京市北京市东城区----》BJSBJSDCQ
                String shortcode = province + city + district;
                String[] strings = PinYin4jUtils.getHeadByString(shortcode);
                shortcode = StringUtils.join(strings, "");  //把数组拼成字符串

                // 城市编码----北京市-----》beijingshi
                String[] strings2 = PinYin4jUtils.stringToPinyin(city);
                String citycode = StringUtils.join(strings2);

                region.setShortcode(shortcode);
                region.setCitycode(citycode);
                list.add(region);
            }
            // 批量保存数据
            regionService.saveBatch(list);
        } catch (Exception e) {
            flag = "0";
        }
        // 向客户端浏览器写回数据
        try {
            ServletActionContext.getResponse().getWriter().print(flag);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "list";
    }

    //分页查寻

    public String pageQuery() throws IOException {
        this.setPage();

        regionService.pageQuery(pageBean);

        String[] arr = new String[]{"detachedCriteria", "currentPage", "pageSize","subareas"};
        toJSON(pageBean,arr);
        return NONE;
    }

    //分区查询的异步请求
    public String findListByAjax() throws IOException {
        List<Region> list = regionService.findAll();

        String[] arr = new String[]{ "subareas", "province", "city",
                "district", "postcode", "shortcode", "citycode"};
        toJSON(list,arr);
        return NONE;
    }

}
